package com.yudaob2b.elong.listener;

import com.yudaob2b.business.service.ElongHotelInventoryService;
import com.yudaob2b.elong.constant.RedisConstant;
import com.yudaob2b.elong.service.HotelInventoryService;
import com.yudaob2b.elong.task.IncrInvTask;
import org.frameworkset.elasticsearch.ElasticSearchHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.sound.midi.Receiver;

/**
 * @author Kidd.Tan
 * @email 298023780@qq.com
 * @date 7/15/18 1:05 AM
 */
@Component
public class RedisReceiverListener implements MessageListener {
    private static Logger logger = LoggerFactory.getLogger(Receiver.class);

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Resource(name = "incrTaskExecutor")
    private ThreadPoolTaskExecutor incrTaskExecutor;

    @Autowired
    private ElongHotelInventoryService elongHotelInventoryService;

    @Autowired
    private HotelInventoryService hotelInventoryService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void onMessage(Message message, byte[] pattern) {
        String patternStr = new String(pattern);
        RedisSerializer<String> valueSerializer = stringRedisTemplate.getStringSerializer();
        String deserialize = valueSerializer.deserialize(message.getBody());
        if (RedisConstant.REDIS_TOPIC_INCR_INVENTORY.equals(patternStr)) {
            IncrInvTask incrInvTask = new IncrInvTask(deserialize, elongHotelInventoryService
                    , hotelInventoryService, redisTemplate
                    , ElasticSearchHelper.getRestClientUtil());
            incrTaskExecutor.execute(incrInvTask);
        }

        //logger.info("收到的mq消息" + deserialize);
    }

}
